// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;
const COLORS = ['#ECE0D5', '#EBDCC2', '#F4A873', '#F18151', '#F1654D', '#F0462D', '#9A8032', '#F4CD50', '#78C93A', '#C9963A', '#C2BC2F', '#E64AA4', '#37C377', '#0CF694', '#35B8B5', '#984DD9']

@ccclass
export default class Grid extends cc.Component {
    private lblNum: cc.Label

    private _num: number = 0;
    public get num(): number { return this._num }
    public set num(val: number) {
        this._num = val;
        this.lblNum.string = val.toString();

        this.setColor();
    }

    private _pos: cc.Vec2
    public get pos(): cc.Vec2 { return this._pos }


    public init(pos: cc.Vec2) {
        this.lblNum = this.node.getComponentInChildren(cc.Label);
        this._pos = pos;

        this.node.active = false;
        this.node.setPosition(pos);
    }

    public reset() {
        this._num = 0;

        this.node.active = false;
        this.node.setPosition(this._pos);
    }

    public stopTween() {
        this.node.stopAllActions();
        this.node.scale = 1;
    }

    private setColor(){
        if (!this.node.active) {
            this.node.active = true;
        }

        this.lblNum.node.color = this._num <= 4 ? cc.Color.BLACK : cc.Color.WHITE;

        let idx = Math.log2(this._num);
        idx = idx == COLORS.length ? COLORS.length - 1 : idx - 1;

        this.node.color = new cc.Color().fromHEX(COLORS[idx]);
    }
    // onLoad () {}

    start() {

    }

    // update (dt) {}
}
